<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Article;
use app\common\model\Banner;
use app\common\model\user\Appoints;
use app\common\model\UserCards;
use think\Config;
use think\Model;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     *
     */
    public function info()
    {
        $time =time();
        $user_id=$this->auth->isLogin()?$this->auth->id:0;
        $data['appoints']=Appoints::where(['status'=>0,'user_id'=>$user_id])->count();
        $data['rights']=UserCards::where(['rights_endtime'=>['<',$time],'status'=>1,'can_uses'=>['>',0],'user_id'=>$user_id])->count();
        $this->success('',$data);
    }



    public function banner()
    {
        $district=$this->request->param('district','');
        $area_id=0;
//        if (!empty($district)){
//            $district_id=db('marea')->where(['name'=>$district,'level'=>3])->value('id');
//            $area_id=$district_id;
//            //  $where['_string'] = "getDistance(lng, lat, {$lng}, {$lat})<{$distance}";
//        }
        $data['banner']=(new Banner())->where(['status'=>1])->order(['weigh'=>'desc'])->select();
//        if(empty($area_id)){
//            ;
//        }else{
//            $where=['status'=>1,'is_all'=>1,'area_id'=>$area_id];
//            $where2=['is_all'=>2,'status'=>1];
//            $data['banner']=(new Banner())
//                ->where(function ($query1) use ( $where) {
//                    $query1->where($where);
//
//                })->whereOr(function ($query) use ($where2) {
//                    if ($where2) {
//                        $query->where($where2);
//                    }
//                })
//                ->order(['weigh'=>'desc'])
//                ->select();
//        }
        foreach ($data['banner'] as $k=>$v){
            $v['image']=cdnurl($v['image'],true);
        }

     $this->success('',$data);
    }



    public function helpList()
    {
        $data=(new Article())->where(['type'=>1,'status'=>1])->order(['weigh'=>'desc'])->paginate();
        $this->success('',$data);
    }

    public function helpDetail()
    {
        $param = $this->request->param();
        if(!isset($param['artitle_id'])){
            $this->error('缺少参数');
        }
        $data =(new Article())->where(['type'=>1,'status'=>1,'id'=>$param['artitle_id']])->find();
        $this->success('',$data);
    }


}
